﻿using System.Collections.Generic;
using FairyGUI;
using UnityEngine;

namespace ZFramework.Runtime
{
    public class FguiUIPackageInfo : IReference
    {
        public enum EnumAtlasLoadStatus
        {
            UnInit,    //未初始化
            Loaded,    //已加载
            Unload,    //已卸载
        }

        private string _pkgName;
        private string _descTextAssetPath;   //描述文件路径
        private string _atlasRootPath;       //图集所在目录
        private bool _loadFlag;              //是否下载好
        private TextAsset _loadedPkgDescTextAsset;
        private List<string> dependecies;
        private int _refCount;
        private UIPackage _package;

        private EnumAtlasLoadStatus _atlasLoadStatus;

        public EnumAtlasLoadStatus AtlasLoadStatus
        {
            get => _atlasLoadStatus;
            set => _atlasLoadStatus = value;
        }

        public string PkgName => _pkgName;

        public UIPackage Package
        {
            get => _package;
            set => _package = value;
        }

        public string DescTextAssetPath
        {
            get => _descTextAssetPath;
            set => _descTextAssetPath = value;
        }
        
        public string AtlasRootPath
        {
            get => _atlasRootPath;
            set => _atlasRootPath = value;
        }
        
        public bool LoadFlag
        {
            get => _loadFlag;
            set => _loadFlag = value;
        }
        
        public TextAsset LoadedPkgDescTextAsset
        {
            get => _loadedPkgDescTextAsset;
            set => _loadedPkgDescTextAsset = value;
        }

        public List<string> Dependecies => dependecies;

        public FguiUIPackageInfo()
        {
            dependecies = new List<string>();   
        }

        public void AddDependence(string depPkgName)
        {
            dependecies.Add(depPkgName);
        }

        public void AddRefCount()
        {
            _refCount += 1;
            if (_atlasLoadStatus == EnumAtlasLoadStatus.Unload)
            {
                _atlasLoadStatus = EnumAtlasLoadStatus.Loaded;
                _package.ReloadAssets();
            }
        }

        public void SubRefCount()
        {
            _refCount -= 1;
            if (_refCount < 0)
            {
                Log.Error("FguiUIPackageInfo引用计数管理出错，_refCount=={0}", _refCount);
            }
        }

        public static FguiUIPackageInfo Create(string pkgName)
        {
            var info = ReferencePool.Acquire<FguiUIPackageInfo>();
            info._pkgName = pkgName;
            info._refCount = 0;
            info._descTextAssetPath = string.Empty;
            info._atlasRootPath = string.Empty;
            info._loadFlag = false;
            info._loadedPkgDescTextAsset = null;
            info._package = null;
            info.dependecies.Clear();
            info._atlasLoadStatus = EnumAtlasLoadStatus.UnInit;
            return info;
        }

        public void Clear()
        {
            _refCount = 0;
            _descTextAssetPath = string.Empty;
            _atlasRootPath = string.Empty;
            _loadFlag = false;
            _loadedPkgDescTextAsset = null;
            _package = null;
            dependecies.Clear();
        }

        public void Release()
        {
            if (_refCount > 0)
                return;
            
            if (_atlasLoadStatus != EnumAtlasLoadStatus.Loaded)
                return;
            _atlasLoadStatus = EnumAtlasLoadStatus.Unload;
            _package.UnloadAssets();
        }
    }
}